package edu.sctu.jsoup.service;

import edu.sctu.jsoup.entity.TourEntity;
import edu.sctu.jsoup.repo.TourRepo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;


@Service
public class TourService {

    @Autowired
    private TourRepo tourRepo;

    public void run() {
        for (int i = (int) tourRepo.count() + 1; i <= 6026; i++) {
            String url = "https://www.tripadvisor.cn/TourismBlog-t" + i + ".html";
//        String url = "https://www.tripadvisor.cn/TourismBlog-t2645.html";
            TourEntity tourEntity = new TourEntity();

            try {
//            Document document = Jsoup.connect(url).get();
                Document document = Jsoup.connect(url)
                        .userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").get();

//                Document document = Jsoup.connect(url).header("User-Agent",
//                "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko
//                /20100316 Firefox/3.6.2").get();
                if (document.select("div.title-text").first() == null) {
                    System.out.println(url + "无");
                    tourEntity.setHtml_content("无游记内容");
                } else {
                    System.out.println(url + "存");
                    tourEntity.setHtml_content(document.toString());
                }

                tourEntity.setHtml_url(url);
                tourEntity.setHtml_time(System.currentTimeMillis());

                tourRepo.save(tourEntity);

                if (i % 1000 == 0)
                    Thread.sleep(60000);
                else if (i % 100 == 0)
                    Thread.sleep(20000);
                else if (i % 50 == 0)
                    Thread.sleep(10000);
//                else if (i % 25 == 0)
//                    Thread.sleep(8000);
//                else if (i % 10 == 0)
//                    Thread.sleep(4000);
//                else if (i % 5 == 0)
//                    Thread.sleep(2000);
//                else if (i % 1 == 0)
//                    Thread.sleep(1000);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("出现异常");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
